package com.saftalk;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.saftalk.db.PMF;
import com.saftalk.db.entity.Family;
import com.saftalk.db.entity.User;
import com.saftalk.ui.UIMessageResult;
import com.saftalk.ui.UIPresenceListResult;
import com.saftalk.ui.UIUserResult;

public class DebugServlet extends BaseServlet {

	
	private static final long serialVersionUID = -390419067525928130L;

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain; charset=UTF-8");

		GsonBuilder builder = new GsonBuilder();
		Gson gson = builder.create();

		if ("/listUsers".equals(req.getPathInfo())) {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query q = pm.newQuery(User.class);
			try {
				List<User> results;
				results = (List<User>) q.execute();

				if (results.isEmpty()) {
					gson.toJson(new UIUserResult(null), resp.getWriter());
				} else {
					
					gson.toJson(convertResults(results), resp.getWriter());
				}

			} catch (Exception e) {

				e.printStackTrace();
				gson.toJson(new UIMessageResult(false, "Failed to get users"), resp.getWriter());
			
			} finally {
				q.closeAll();
			}

		}
		
		if ("/printSession".equals(req.getPathInfo())) {
			Object sessionInfo = req.getSession().getAttribute("user");
		
			UIUserResult user = (UIUserResult)sessionInfo;
			gson.toJson(user, resp.getWriter());

		}
		// Add missing property to all users
		if ("/addLastSeen".equals(req.getPathInfo())) {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query q = pm.newQuery(User.class);
			List<User> queryResults = null;
			try {
				queryResults = (List<User>) q.execute();	
				for (User user : queryResults)
				{
					user.updateLastSeen();
					pm.makePersistent(user);
				}	
			} catch (Exception e) {
				e.printStackTrace();
				gson.toJson(new UIMessageResult(false, "Failed to update users"),
						resp.getWriter());
			} finally {
				q.closeAll();
			}
			
			gson.toJson(new UIMessageResult(true, "Updated"),resp.getWriter());
		}
		
		if ("/addPhotos".equals(req.getPathInfo())) {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query q = pm.newQuery(User.class);
			List<User> queryResults = null;
			try {
				queryResults = (List<User>) q.execute();	
				for (User user : queryResults)
				{
					user.setPhotoId("AMIfv96TypTdMwrZhnsBZk2qm9xYO84Dwb7GKWW_0S0NZGEVq62OFOdQTTwVewCd4M3l-e9XfdnthvQLGk6heZAJGVb9R86_LHEyVIdze5FOvA7lf14K_vY-yxOE9xXFvLrvnqrGjD3OiGU8128hQT3gEGfgQSyJcshLi_5PDuvefRBcE7DB3XE");
					pm.makePersistent(user);
				}	
			} catch (Exception e) {
				e.printStackTrace();
				gson.toJson(new UIMessageResult(false, "Failed to update users"),
						resp.getWriter());
			} finally {
				q.closeAll();
			}
			
			gson.toJson(new UIMessageResult(true, "Updated"),resp.getWriter());
		}
		
		if ("/addSenior".equals(req.getPathInfo())) {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query q = pm.newQuery(User.class);
			List<User> queryResults = null;
			try {
				queryResults = (List<User>) q.execute();	
				for (User user : queryResults)
				{
					user.setSenior(false);
					pm.makePersistent(user);
				}	
			} catch (Exception e) {
				e.printStackTrace();
				gson.toJson(new UIMessageResult(false, "Failed to update users"),
						resp.getWriter());
			} finally {
				q.closeAll();
			}
			
			gson.toJson(new UIMessageResult(true, "Updated"),resp.getWriter());
		}
		
		if ("/addAdmin".equals(req.getPathInfo())) {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query q = pm.newQuery(User.class);
			List<User> queryResults = null;
			try {
				queryResults = (List<User>) q.execute();	
				for (User user : queryResults)
				{
					user.setAdmin(false);
					pm.makePersistent(user);
				}	
			} catch (Exception e) {
				e.printStackTrace();
				gson.toJson(new UIMessageResult(false, "Failed to update users"),
						resp.getWriter());
			} finally {
				q.closeAll();
			}
			
			gson.toJson(new UIMessageResult(true, "Updated"),resp.getWriter());
		}
	}
	
	private List<UIUserResult> convertResults(List<User> users) throws Exception {
		
		List<UIUserResult> uiList = new ArrayList<UIUserResult>();
		for (User user: users) {
			uiList.add(new UIUserResult(user));
		}
		return uiList;
	}
}
